//
//  YCHomeViewModel.swift
//  weiBoSwift
//
//  Created by 刘胤辰 on 16/7/23.
//  Copyright © 2016年 it.com. All rights reserved.
//

import UIKit

class YCHomeViewModel: NSObject {

    //全局访问点
    lazy var statusLists : [YCHomeStatus] = [YCHomeStatus]()
    
    //加载home网络数据
    func loadWebHomeData(isPullUp:Bool,since_id:Int64,max_id:Int64,isSuccess:(isSuccess:Bool)->()){
        
        let url="https://api.weibo.com/2/statuses/home_timeline.json"
        let pram=[ "access_token" : "2.00nDZ86GRDfnSC4b0b88f871174WQD",
            "since_id" : "\(since_id)",
            "max_id" : "\(max_id)"
        ]
        
        //请求数据
        YCNetWorkTools.shareTools.request(YCNetWorkType.GET, urlstring: url, parameters: pram, success: { (response) -> () in
            
            guard let res=response!["statuses"] as? [[String : AnyObject]] else{
                isSuccess(isSuccess: false)
                return
            }
            //字典转模型
            var tem = [YCHomeStatus]()
            for dict in res{
                
                let status=YCHomeStatus(dict: dict)
                tem.append(status)
            }
            
            //设置数据源
            if isPullUp {
                self.statusLists=self.statusLists + tem
            }else{
                self.statusLists=tem + self.statusLists
            }
            
            isSuccess(isSuccess: true)

            }) { (error) -> () in
                
                isSuccess(isSuccess: false)
                print(error)
            
        }
    }
    

}
